package org.yczbj.ycvideoplayer.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import com.yc.kernel.factory.PlayerFactory;
import com.yc.kernel.impl.ijk.IjkPlayerFactory;
import com.yc.kernel.impl.media.MediaPlayerFactory;
import com.yc.video.config.ConstantKeys;
import com.yc.video.config.VideoPlayerConfig;
import com.yc.video.player.VideoViewManager;
import com.yc.video.tool.BaseToast;
import com.yc.video.tool.PlayerUtils;

import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.ability.AdAbility;
import org.yczbj.ycvideoplayer.ability.ClarityAbility;
import org.yczbj.ycvideoplayer.ability.ContinuousVideoAbility;
import org.yczbj.ycvideoplayer.ability.DanmuAbility;
import org.yczbj.ycvideoplayer.ability.MultipleAbility;
import org.yczbj.ycvideoplayer.ability.NormalAbility;
import org.yczbj.ycvideoplayer.ability.PipAbility;
import org.yczbj.ycvideoplayer.ability.PipListAbility;
import org.yczbj.ycvideoplayer.ability.TestFullAbility;
import org.yczbj.ycvideoplayer.ability.TestListAbility;
import org.yczbj.ycvideoplayer.ability.TestSurfaceAbility;
import org.yczbj.ycvideoplayer.ability.TinyScreenAbility;
import org.yczbj.ycvideoplayer.utils.JumpUtil;

import java.lang.reflect.Field;

import timber.log.Timber;

/**
 * MainAbilitySlice
 *
 * @since 2021-05-12
 */
public class MainAbilitySlice extends AbilitySlice
    implements Component.ClickedListener {
    private Text mTvTitle;
    private Text mTv11;
    private Text mTv12;
    private Text mTv13;
    private Text mTv21;
    private Text mTv22;
    private Text mTv23;
    private Text mTv31;
    private Text mTv32;
    private Text mTv33;
    private Text mTv41;
    private Text mTv42;
    private Text mTv43;
    private Text mTv61;
    private Text mTv62;
    private Text mTv63;
    private Text mTv64;
    private Text mTv65;
    private Text mTv66;
    private Text mTv71;
    private Text mTv81;
    private Text mTv101;
    private Text mTv111;
    private Text mTv131;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initFindViewById();
        initListener();

        // 检测当前是用的哪个播放器
        Object factory = PlayerUtils.getCurrentPlayerFactory();
        if (factory instanceof IjkPlayerFactory) {
            mTvTitle.setText("视频内核：" + " (IjkPlayer)");
        } else if (factory instanceof MediaPlayerFactory) {
            mTvTitle.setText("视频内核：" + " (MediaPlayer)");
        } else {
            mTvTitle.setText("视频内核：" + " (unknown)");
        }
    }

    private void initFindViewById() {
        mTvTitle = (Text) findComponentById(ResourceTable.Id_tv_title);
        mTv11 = (Text) findComponentById(ResourceTable.Id_tv_1_1);
        mTv12 = (Text) findComponentById(ResourceTable.Id_tv_1_2);
        mTv13 = (Text) findComponentById(ResourceTable.Id_tv_1_3);
        mTv21 = (Text) findComponentById(ResourceTable.Id_tv_2_1);
        mTv22 = (Text) findComponentById(ResourceTable.Id_tv_2_2);
        mTv23 = (Text) findComponentById(ResourceTable.Id_tv_2_3);
        mTv31 = (Text) findComponentById(ResourceTable.Id_tv_3_1);
        mTv32 = (Text) findComponentById(ResourceTable.Id_tv_3_2);
        mTv33 = (Text) findComponentById(ResourceTable.Id_tv_3_3);
        mTv41 = (Text) findComponentById(ResourceTable.Id_tv_4_1);
        mTv42 = (Text) findComponentById(ResourceTable.Id_tv_4_2);
        mTv43 = (Text) findComponentById(ResourceTable.Id_tv_4_3);
        mTv61 = (Text) findComponentById(ResourceTable.Id_tv_6_1);
        mTv62 = (Text) findComponentById(ResourceTable.Id_tv_6_2);
        mTv63 = (Text) findComponentById(ResourceTable.Id_tv_6_3);
        mTv64 = (Text) findComponentById(ResourceTable.Id_tv_6_4);
        mTv65 = (Text) findComponentById(ResourceTable.Id_tv_6_5);
        mTv66 = (Text) findComponentById(ResourceTable.Id_tv_6_6);
        mTv71 = (Text) findComponentById(ResourceTable.Id_tv_7_1);
        mTv81 = (Text) findComponentById(ResourceTable.Id_tv_8_1);
        mTv101 = (Text) findComponentById(ResourceTable.Id_tv_10_1);
        mTv111 = (Text) findComponentById(ResourceTable.Id_tv_11_1);
        mTv131 = (Text) findComponentById(ResourceTable.Id_tv_13_1);
    }

    private void initListener() {
        mTv11.setClickedListener(this);
        mTv12.setClickedListener(this);
        mTv13.setClickedListener(this);
        mTv21.setClickedListener(this);
        mTv22.setClickedListener(this);
        mTv23.setClickedListener(this);
        mTv31.setClickedListener(this);
        mTv32.setClickedListener(this);
        mTv33.setClickedListener(this);
        mTv41.setClickedListener(this);
        mTv42.setClickedListener(this);
        mTv43.setClickedListener(this);
        mTv61.setClickedListener(this);
        mTv62.setClickedListener(this);
        mTv63.setClickedListener(this);
        mTv64.setClickedListener(this);
        mTv65.setClickedListener(this);
        mTv66.setClickedListener(this);
        mTv71.setClickedListener(this);
        mTv81.setClickedListener(this);
        mTv101.setClickedListener(this);
        mTv111.setClickedListener(this);
        mTv131.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void setChangeVideoType(@ConstantKeys.PlayerType
                                        int type) {
        if (type == ConstantKeys.VideoPlayerType.TYPE_EXO) {
            mTvTitle.setText("视频内核：" + " (ExoPlayer)");
            return;
        }
        // 切换播放核心，不推荐这么做，我这么写只是为了方便测试
        VideoPlayerConfig config = VideoViewManager.getConfig();
        try {
            Field mPlayerFactoryField = config.getClass().getDeclaredField("mPlayerFactory");
            mPlayerFactoryField.setAccessible(true);
            PlayerFactory playerFactory = null;
            switch (type) {
                case ConstantKeys.VideoPlayerType.TYPE_IJK:
                    playerFactory = IjkPlayerFactory.create();
                    mTvTitle.setText("视频内核：" + " (IjkPlayer)");
                    break;
                case ConstantKeys.VideoPlayerType.TYPE_EXO:
                    mTvTitle.setText("视频内核：" + " (ExoPlayer)");
                    break;
                case ConstantKeys.VideoPlayerType.TYPE_NATIVE:
                    playerFactory = MediaPlayerFactory.create();
                    mTvTitle.setText("视频内核：" + " (MediaPlayer)");
                    break;
                case ConstantKeys.VideoPlayerType.TYPE_RTC:
                    break;
                default:
                    break;
            }
            mPlayerFactoryField.set(config, playerFactory);
        } catch (Exception e) {
            Timber.i(e.getMessage());
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_1_1:
                // 切换ijk
                setChangeVideoType(ConstantKeys.VideoPlayerType.TYPE_IJK);
                break;
            case ResourceTable.Id_tv_1_2:
                // 切换exo
                setChangeVideoType(ConstantKeys.VideoPlayerType.TYPE_EXO);
                break;
            case ResourceTable.Id_tv_1_3:
                // 切换原生
                setChangeVideoType(ConstantKeys.VideoPlayerType.TYPE_NATIVE);
                break;
            case ResourceTable.Id_tv_2_1:
            case ResourceTable.Id_tv_2_2:
            case ResourceTable.Id_tv_2_3:
                BaseToast.showRoundRectToast("待完善");
                break;
            case ResourceTable.Id_tv_3_1:
                JumpUtil.jumpToAbility(this, NormalAbility.class);
                break;
            case ResourceTable.Id_tv_3_2:
                JumpUtil.jumpToAbility(this, TestFullAbility.class);
                break;
            case ResourceTable.Id_tv_3_3:
                JumpUtil.jumpToAbility(this, MultipleAbility.class);
                break;
            case ResourceTable.Id_tv_4_1:
                JumpUtil.jumpToAbility(this, PipAbility.class);
                break;
            case ResourceTable.Id_tv_4_2:
                JumpUtil.jumpToAbility(this, PipListAbility.class);
                break;
            case ResourceTable.Id_tv_4_3:
                JumpUtil.jumpToAbility(this, TinyScreenAbility.class);
                break;
            case ResourceTable.Id_tv_6_1:
                startAbility(0);
                break;
            case ResourceTable.Id_tv_6_2:
                startAbility(1);
                break;
            case ResourceTable.Id_tv_6_3:
                startAbility(2);
                break;
            case ResourceTable.Id_tv_6_4:
                startAbility(3);
                break;
            case ResourceTable.Id_tv_6_5:
                startAbility(4);
                break;
            case ResourceTable.Id_tv_6_6:
                startAbility(5);
                break;
            case ResourceTable.Id_tv_7_1:
                JumpUtil.jumpToAbility(this, DanmuAbility.class);
                break;
            case ResourceTable.Id_tv_8_1:
                JumpUtil.jumpToAbility(this, AdAbility.class);
                break;
            case ResourceTable.Id_tv_10_1:
                JumpUtil.jumpToAbility(this, ContinuousVideoAbility.class);
                break;
            case ResourceTable.Id_tv_11_1:
                JumpUtil.jumpToAbility(this, ClarityAbility.class);
                break;
            case ResourceTable.Id_tv_13_1:
                JumpUtil.jumpToAbility(this, TestSurfaceAbility.class);
                break;
            default:
                break;
        }
    }

    private void startAbility(int param) {
        Intent intent5 = new Intent();
        intent5.setOperation(new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(TestListAbility.class)
            .build());
        intent5.setParam("type", param);
        startAbility(intent5);
    }
}
